package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.pojo.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author:篍
 * @Date: 2025/08/15 22:51
 * @Description: 菜品管理控制器
 */
@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品管理")
public class DishController {

    @Autowired
    DishService dishService;

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> page(@ApiParam("菜品分页查询参数") DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品分页查询：{}", dishPageQueryDTO);
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 新增菜品
     *
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result add(@ApiParam("菜品信息") @RequestBody DishDTO dishDTO) {
        log.info("新增菜品：{}", dishDTO);
        dishService.add(dishDTO);
        return Result.success();
    }

    /**
     * 菜品起售/停售
     *
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("菜品起售/停售")
    public Result updateStatus(@ApiParam("菜品状态") @PathVariable("status") Integer status,
                               @ApiParam("菜品id") @RequestParam("id") Long id) {
        log.info("菜品状态：{}，菜品id：{}", status, id);
        dishService.updateStatus(id, status);
        return Result.success();
    }

    /**
     * 根据id查询菜品
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> getById(@ApiParam("菜品id") @PathVariable("id") Long id) {
        log.info("查询菜品id：{}", id);
        DishVO dishVO = dishService.getById(id);
        return Result.success(dishVO);
    }

    /**
     * 修改菜品
     *
     * @param dishDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改菜品")
    public Result update(@ApiParam("菜品信息") @RequestBody DishDTO dishDTO) {
        log.info("修改菜品：{}", dishDTO);
        dishService.update(dishDTO);
        return Result.success();
    }

    /**
     * 批量删除菜品
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除菜品")
    public Result delete(@ApiParam("菜品id集合") @RequestParam("ids") List<Long> ids) {
        log.info("批量删除菜品：{}", ids);
         dishService.deleteByIds(ids);
        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result getDishListByCategoryId(@ApiParam("分类id") Long categoryId,@ApiParam("菜品名称") String name){
        List<Dish> dishList = dishService.getDishListByCategoryId(categoryId, name);
        return Result.success(dishList);
    }


}
